//
//  EmojiKeyBoardView.swift
//  EmojiKeyBoard
//
//  Created by macs on 15/11/20.
//  Copyright © 2015年 BlackTail. All rights reserved.
//

import UIKit
let emojiCellId = "emojiCellId"
let toolBarHeight :CGFloat = 36

class EmojiKeyBoardView: UIView  {
    
    var selectEmoji : ((em : Emoji)->())?
    
    var emojiPackages = EmojiManage().packages
    @objc func barBtnDidClick (barBtn : UIBarButtonItem) {
        
        let indexpath = NSIndexPath(forItem: 0, inSection: barBtn.tag)
        collection.scrollToItemAtIndexPath(indexpath, atScrollPosition: .Left, animated: false)
        
    }
    
    //3.重写构造方法
    init(selectEmoji : (em : Emoji)->()) {
        self.selectEmoji = selectEmoji
        let rect = CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, 220)
        super.init(frame: rect);
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //1.懒加载控件
    lazy var toolBar = UIToolbar()
    lazy var collection : UICollectionView = {
        
        let layout = UICollectionViewFlowLayout()
        let itemW = UIScreen.mainScreen().bounds.width / 7
        layout.itemSize = CGSize(width: itemW, height: itemW)
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.scrollDirection = .Horizontal
        
        let edgeInset = (self.bounds.height - 3 * itemW - toolBarHeight) / 4
        layout.sectionInset = UIEdgeInsets(top: edgeInset, left: 0, bottom: edgeInset, right: 0)
        
        let cv = UICollectionView(frame: CGRectZero, collectionViewLayout: layout)
        
        cv.backgroundColor = UIColor.whiteColor()
        //注册 cell
        cv .registerClass(EmojiKeyBoardCell.self, forCellWithReuseIdentifier: emojiCellId)
        
        cv.dataSource = self
        
        cv.delegate = self
        
        cv.pagingEnabled = true
        
        return cv
    } ()
    
}

//设置 collecttionView 数据源
extension EmojiKeyBoardView : UICollectionViewDataSource, UICollectionViewDelegate {
    
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return emojiPackages.count
    }
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return emojiPackages[section].emoticons.count
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collection.dequeueReusableCellWithReuseIdentifier(emojiCellId, forIndexPath: indexPath) as! EmojiKeyBoardCell
        
        //        cell.backgroundColor =  indexPath.item % 2 == 1 ? UIColor.redColor() : UIColor.orangeColor()
        
        cell.emoji = emojiPackages[indexPath.section].emoticons[indexPath.item]
        
        return cell
    }
    
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        
        let em = emojiPackages[indexPath.section].emoticons[indexPath.item]
        selectEmoji?(em: em)
        
    }
}


//setupUI
extension EmojiKeyBoardView {
    
    //2.添加子控件,设置约束
    func setupUI() {
        addSubview(toolBar)
        addSubview(collection)
        
        toolBar.snp_makeConstraints { (make) -> Void in
            make.bottom.left.right.equalTo(self)
            make.height.equalTo(toolBarHeight)
        }
        
        collection.snp_makeConstraints { (make) -> Void in
            make.left.right.top.equalTo(self)
            make.bottom.equalTo(toolBar.snp_top)
        }
        setupToolBar()
        
    }
    
    func setupToolBar () {
        
        toolBar.tintColor = UIColor.lightGrayColor()
        
        var items = [UIBarButtonItem]()
        var index = 0
        for title in emojiPackages {
            
            let barBtn = UIBarButtonItem(title: title.group_name_cn, style: .Plain, target: self, action: "barBtnDidClick:");
            barBtn.tag = index++
            items.append(barBtn)
            
            let space = UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: nil, action: nil)
            items.append(space)
        }
        //删除最后一个弹簧
        items.removeLast()
        toolBar.items = items
    }
    
}

